package com.szgr.baoyin.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.szgr.baoyin.R;

/**
 * Created by yang on 15/9/25.
 */
public class DialogUtils {
    private DialogUtils(){}

    public static Dialog showCustomerDialog(Context context,View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        return builder.create();
    }

    public static Dialog showCustomerDialog(Context context,String msg,String confirm,
                                            View.OnClickListener l1){

        return showCustomerDialog(context,msg,confirm,null,l1,null);
    }

    public static Dialog showCustomerDialog(Context context,String msg,String confirm,
                                            String cancel,View.OnClickListener l1,View.OnClickListener l2){
        return showCustomerDialog(context,msg,confirm,cancel,null,l1,l2,null);
    }

    public static Dialog showCustomerDialog(Context context,String msg,String confirm,
                                            String cancel,String once,View.OnClickListener l1,View.OnClickListener l2,View.OnClickListener l3){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_protect_content_view,null,false);
        TextView tv_msg = (TextView) view.findViewById(R.id.dialog_show_msg);
        tv_msg.setText(msg);
        Button btn_know = (Button) view.findViewById(R.id.protect_buy_now);
        btn_know.setText(confirm);
        btn_know.setOnClickListener(l1);

        Button btn_see = (Button) view.findViewById(R.id.protect_buy_next);
        if(cancel == null){
            btn_see.setVisibility(View.GONE);
        }else {
            btn_see.setText(cancel);
        }
        if(l2 != null) {
            btn_see.setOnClickListener(l2);
        }

        Button btn_once = (Button) view.findViewById(R.id.protect_buy_once);
        if(once == null){
            btn_once.setVisibility(View.GONE);
        }else{
            btn_once.setText(once);
        }
        if(l3 != null){
            btn_once.setOnClickListener(l3);
        }

        builder.setView(view);
        return builder.create();
    }

    private static CustomProgressDialog loadingDialog = null;

    /**
     * 显示加载框
     * @param mContext
     * @param msg
     * @return
     */
    public static CustomProgressDialog showLoadingDialog(Context mContext,
                                                         String msg) {

        return showLoadingDialog(mContext, msg, true);
    }


    /**
     * 显示加载框
     * @param mContext
     * @param msg
     * @param cancelable
     * @return
     */
    public static CustomProgressDialog showLoadingDialog(Context mContext,
                                                         String msg, boolean cancelable) {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        loadingDialog = CustomProgressDialog.createDialog(mContext, cancelable);
        loadingDialog.setMessage(msg);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
        return loadingDialog;
    }

    /**
     * 隐藏加载框
     */
    public static void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }
}
